{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 60\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Write a program to compute:_**\n",
    "\n",
    "\n",
    "> $f(n)=f(n-1)+100$ when n>0\n",
    "\n",
    "> and $f(0)=0$\n",
    "\n",
    "\n",
    "> **_with a given n input by console (n>0)._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following n is given as input to the program:_**\n",
    "\n",
    "\n",
    "> 5\n",
    "\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "\n",
    "> 500\n",
    "\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_We can define recursive function in Python._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(n):\n",
    "    if n == 0:\n",
    "        return 0\n",
    "    return f(n - 1) + 100\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(f(n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 61\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_The Fibonacci Sequence is computed based on the following formula:_**\n",
    "\n",
    "\n",
    "> $f(n)=0$ if n=0\n",
    "\n",
    "> $f(n)=1$ if n=1\n",
    "\n",
    "> $f(n)=f(n-1)+f(n-2)$ if n>1\n",
    "\n",
    "\n",
    "> **_Please write a program to compute the value of f(n) with a given n input by console._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following n is given as input to the program:_**\n",
    "\n",
    "\n",
    "> 7\n",
    "\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "\n",
    "> 13\n",
    "\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_We can define recursive function in Python._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(n):\n",
    "    if n < 2:\n",
    "        return n\n",
    "    return f(n - 1) + f(n - 2)\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(f(n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 62\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_The Fibonacci Sequence is computed based on the following formula:_**\n",
    "\n",
    "\n",
    "> $f(n)=0$ if n=0\n",
    "\n",
    "> $f(n)=1$ if n=1\n",
    "\n",
    "> $f(n)=f(n-1)+f(n-2)$ if n>1\n",
    "\n",
    "\n",
    "> **_Please write a program to compute the value of f(n) with a given n input by console._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following n is given as input to the program:_**\n",
    "\n",
    "> 7\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "> 0,1,1,2,3,5,8,13\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_We can define recursive function in Python.\n",
    "> Use list comprehension to generate a list from an existing list.\n",
    "> Use string.join() to join a list of strings._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(n):\n",
    "    if n < 2:\n",
    "        fibo[n] = n\n",
    "        return fibo[n]\n",
    "    fibo[n] = f(n - 1) + f(n - 2)\n",
    "    return fibo[n]\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "fibo = [0] * (n + 1)  # initialize a list of size (n+1)\n",
    "f(n)  # call once and it will set value to fibo[0-n]\n",
    "fibo = [str(i) for i in fibo]  # converting integer data to string type\n",
    "ans = \",\".join(fibo)  # joining all string element of fibo with ',' character\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 63\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following n is given as input to the program:_**\n",
    "\n",
    "> 10\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "> 0,2,4,6,8,10\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use yield to produce the next value in generator._**\n",
    "\n",
    "---\n",
    "\n",
    "**Solution:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def EvenGenerator(n):\n",
    "    i = 0\n",
    "    while i <= n:\n",
    "        if i % 2 == 0:\n",
    "            yield i\n",
    "        i += 1\n",
    "\n",
    "\n",
    "n = int(raw_input())\n",
    "values = []\n",
    "for i in EvenGenerator(n):\n",
    "    values.append(str(i))\n",
    "\n",
    "print \",\".join(values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**OR**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution by: StartZer0\n",
    "n = int(input())\n",
    "\n",
    "for i in range(0, n + 1, 2):\n",
    "    if i < n - 1:\n",
    "        print(i, end=\",\")\n",
    "    else:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 64\n",
    "\n",
    "### **Question**\n",
    "\n",
    "> **_Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console._**\n",
    "\n",
    "> **_Example:\n",
    "> If the following n is given as input to the program:_**\n",
    "\n",
    "> 100\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "> 0,35,70\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints\n",
    "\n",
    "> **_Use yield to produce the next value in generator._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate(n):\n",
    "    for i in range(n + 1):\n",
    "        if (\n",
    "            i % 35 == 0\n",
    "        ):  # 5*7 = 35, if a number is divisible by a & b then it is also divisible by a*b\n",
    "            yield i\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "resp = [str(i) for i in generate(n)]\n",
    "print(\",\".join(resp))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
